#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
   @Author: 叶建伟
   @Email: yejw46507@hundsun.com
   @Time: 2023/7/7 16:06
   @File: multiprocessing_insert.py
"""

from multiprocessing import cpu_count, Pool

from DataTest.databases import MysqlUtil
from DataTest.sqlmontage.SQLMontage import get_sql_key, montage_sql_value, get_sql
from DataTest.sqlmontage.clientInfo import client_info_list
import datetime

# 定义全局变量num_threads线程数量，num_inserts插入数据总量，batch_size单次插入数量

num_threads = 8
num_inserts = 100000
batch_size = 50
table_name = 'fnd_client'

db = MysqlUtil('root', 'fxhg@1234', '10.20.181.116', port=33061)


# 定义线程执行的函数：SQL拼接
def sql_assemble(thread_id, db):
    now = datetime.datetime.now()
    formatted_datetime = now.strftime("%m%d%H")
    temp = formatted_datetime + '00000000'
    table_value = get_sql(table=table_name)
    table_key = get_sql_key(table=table_value)
    for i in range(num_inserts//batch_size//num_threads):
        sql = table_key
        for j in range(batch_size):
            sql_list = client_info_list()
            id = str(thread_id * (num_inserts // num_threads) + i * batch_size + j)
            client_id = temp[:14-len(id)]+id
            sql_list.update({'client_id':client_id,'inner_client_id':client_id})
            values = montage_sql_value(table=table_value, sql_list=sql_list)
            sql += values
            if j < batch_size - 1:
                sql += ","
        db.execsql(sql)

def test_txt():
    for i in range(100):
        print(f'这是第{i}+{i}个错误')

if __name__ == '__main__':

    startTime = datetime.datetime.now()
    p = Pool(cpu_count())  # cpu_count 查询当前设备进程数，我的是八核，所以下面分了8个进程
    for i in range(num_threads):
        # p.apply_async(sql_assemble, args=[i, db])
        p.apply_async(test_txt, args=[i, db])


    p.close()
    p.join()
    endTime = datetime.datetime.now()
    print(endTime - startTime)
